<?php
// +----------------------------------------------------------------------
// | 商品管理 [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2019 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 小夏 < 449134904@qq.com>
// +----------------------------------------------------------------------
namespace app\admin\controller;

use cmf\controller\AdminBaseController;
use function PHPSTORM_META\type;
use think\Db;
use think\facade\Request;
use app\admin\model\AdminMenuModel;

class GoodsController extends AdminBaseController
{

    public function initialize()
    {
        $this->cate_model = Db::name("Cate");
		$this->deal_model = Db::name("Goods");
        $this->room_model = Db::name("Room");
        parent::initialize();
    }
	/*库房管理*/
	public function front(){
		$this->assign("results",'');
		$this->assign("page",'');
		return $this->fetch();
	}
	/*库房管理*/
	public function addfront()
    {
        $this->assign("results", '');
        $this->assign("page", '');
        return $this->fetch();
    }
	/**
	 * 获取商品一级分类信息
	 **/
	public function getCate($pid = 0,$type=0){
		$where = array(
			'type'		=> 2,
			'if_show'	=> 1,
			'is_delete'	=> 0,
			'parent_id'	=> 0
		);
		if($type==1){
			$where+=array(	'mark'	=>'exchange');
		}else{
			$where+=array(	'mark'	=>'normal');
			}
		$cates = $this->cate_model->field('id,cate_name')->where($where)->order("sort_order ASC,id DESC")->select();
		$this->assign("cates", $cates);
		
		if($pid){
			$where['parent_id'] = $pid;
			$cates = $this->cate_model->field('id,cate_name')->where($where)->order("sort_order ASC,id DESC")->select();
			$this->assign("childCates", $cates);
		}
		
	}
	//获取库房
	public function get_room(){
		$where=array('if_show'=>1,'is_delete'=>0);
		
		$rooms=$this->room_model->where($where)->select();
		$this->assign('rooms',$rooms);
	}
	/*商品管理*/
	public function index(){
	    $type = $this->request->param("type",0);
		$this->assign("type",empty($type) ? '' : $type);
        $rooms=$this->room_model->where(['if_show'=>1,'is_delete'=>0])->select();
        $this->assign('rooms',$rooms);
		$where = ['d.type'=>$type,'d.is_delete'=>0,'d.if_show'=>1];
		//商品名称
        $data = $this->request->param('goods_name','');
        $room = $this->request->param("room",0);
        $this->assign('room',$room);
        empty($room)?$where:($where['r.id']=$room);
        //取库房数据
        $results = $this->deal_model
            ->alias("d")
            ->field("d.*,c.cate_name,r.room_name")
            ->join("dj_cate c","d.cate_p_id = c.id","left")
            ->join("dj_room r","r.id = d.room","left")
            ->where('d.goods_name','like','%'.$data.'%')->where($where)->order('d.listorder', 'ASC')
            ->paginate(12);
        $this->assign("results",$results);

        $goods_name_menu = "前台商品";
        $goods_name_add = "添加商品";
        $goods_name_edit = "编辑商品";
        if($type == 1){
            $goods_name_menu = "物料库管理";
            $goods_name_add = "添加物料";
            $goods_name_edit = "编辑物料";
        }else if($type == 2){
            $goods_name_menu = "固定资产管理";
            $goods_name_add = "添加固定资产";
            $goods_name_edit = "编辑固定资产";
        }
        $this->assign("goods_name_menu", $goods_name_menu);
        $this->assign("goods_name_add", $goods_name_add);
        $this->assign("goods_name_edit", $goods_name_edit);
        // 页面传值
        $page = $results->render();
        $this->assign("page", $page);
		return $this->fetch();
	}
    /*商品添加*/
    public function add(){
        $this->getCate();
        $this->get_room();
        $type = $this->request->param("type",'');
        $this->assign("type",empty($type) ? '' : $type);
        $this->assign("results",'');
        $this->assign("page",'');

        $goods_name_menu = "前台商品";
        $goods_name_add = "添加商品";
        $goods_name_edit = "编辑商品";
        if($type == 1){
            $goods_name_menu = "物料库管理";
            $goods_name_add = "添加物料";
            $goods_name_edit = "编辑物料";
        }else if($type == 2){
            $goods_name_menu = "固定资产管理";
            $goods_name_add = "添加固定资产";
            $goods_name_edit = "编辑固定资产";
        }
        $this->assign("goods_name_menu", $goods_name_menu);
        $this->assign("goods_name_add", $goods_name_add);
        $this->assign("goods_name_edit", $goods_name_edit);


        $goods_num = "GC-".random_int(0001, 9999);
        $this->assign("goods_num", $goods_num);

        return $this->fetch();
    }
    /*
     * 商品添加
     */
    public function add_post(){
        $data = $this->request->param();
        $money_reg = '/^[0-9]\d*|^[0-9]\d*.\d+[1-9]$/';
        if(!preg_match($money_reg, $data['price'])){
            $this->error('金额格式错误');
        }
        if(!empty($data['stock'])){
            if(!preg_match("/^[1-9][0-9]*$/",$data['stock'])){
                $this->error('数量只能为正整数');
            }
        }

        $data['add_user'] = cmf_get_current_admin_id();
        $data['add_time'] = time();
        $data['last_time'] = time();
        $this->deal_model->insertGetId($data);
        $this->success("添加成功", url(Request::controller().'/index',['type'=>$data['type']]));
    }
    /*商品添加*/
    public function edit(){
        $this->getCate();
        $this->get_room();
        $type = $this->request->param("type",'');
        $id = $this->request->param("id",0,'intval');
        $where = array(
            'id'	=> $id
        );
        $result = $this->deal_model->where($where)->find();
        $this->assign($result);
        $this->assign("type",empty($type) ? '' : $type);

        $goods_name_menu = "前台商品";
        $goods_name_add = "添加商品";
        $goods_name_edit = "编辑商品";
        if($type == 1){
            $goods_name_menu = "物料库管理";
            $goods_name_add = "添加物料";
            $goods_name_edit = "编辑物料";
        }else if($type == 2){
            $goods_name_menu = "固定资产管理";
            $goods_name_add = "添加固定资产";
            $goods_name_edit = "编辑固定资产";
        }
        $this->assign("goods_name_menu", $goods_name_menu);
        $this->assign("goods_name_add", $goods_name_add);
        $this->assign("goods_name_edit", $goods_name_edit);


        return $this->fetch();
    }
    /**
     * 修改保存
     **/
    public function edit_post(){
        if ($this->request->isPost()) {
            $arrData  = $this->request->post();
            $arrData['last_time'] = time();
            $money_reg = '/^[0-9]\d*|^[0-9]\d*.\d+[1-9]$/';
            if(!preg_match($money_reg, $arrData['price'])){
                $this->error('金额格式错误');
            }
            if(!empty($arrData['stock'])){
                if(!preg_match("/^[1-9][0-9]*$/",$arrData['stock'])){
                    $this->error('数量只能为正整数');
                }
            }
            $this->deal_model->update($arrData);
            $this->success(lang("EDIT_SUCCESS"), url(Request::controller().'/index',['type'=>$arrData['type']]));
        }
    }
	/*记录*/
	public function log(){
		$this->assign("results",'');
		$this->assign("page",'');
		return $this->fetch("log");
	}
	
}